2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_spi_ex1_slave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //*****************************************************************************
70 //
71 //*****************************************************************************
72 #include "driverlib.h"
73 
74 uint8_t transmitData = 0x01, receiveData = 0x00;
75 
76 void main(void)
77 {
78  //Stop watchdog timer
79  WDT_A_hold(WDT_A_BASE);
80 
81  // Configure SPI pins
82  // Configure Pins for UCB0CLK, UCB0TXD/UCB0SIMO and UCB0RXD/UCB0SOMI
83  /*
84 
85  * Select Port 5
86  * Set Pin 1, Pin 2 and Pin 3 to input with function.
87  */
88  GPIO_setAsPeripheralModuleFunctionInputPin(
89  GPIO_PORT_P5,
90  GPIO_PIN1 + GPIO_PIN2 + GPIO_PIN3,
91  GPIO_PRIMARY_MODULE_FUNCTION
92  );
93 
94  /*
95  * Disable the GPIO power-on default high-impedance mode to activate
96  * previously configured port settings
97  */
98  PMM_unlockLPM5();
99 
100  //Initialize slave to MSB first, inactive high clock polarity and 3 wire SPI
101  EUSCI_B_SPI_initSlaveParam param = {0};
102  param.msbFirst = EUSCI_B_SPI_MSB_FIRST;
103  param.clockPhase = EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
104  param.clockPolarity = EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
105  param.spiMode = EUSCI_B_SPI_3PIN;
106  EUSCI_B_SPI_initSlave(EUSCI_B0_BASE, &param);
107 
108  //Enable SPI Module
109  EUSCI_B_SPI_enable(EUSCI_B0_BASE);
110 
111  EUSCI_B_SPI_clearInterrupt(EUSCI_B0_BASE,
112  EUSCI_B_SPI_RECEIVE_INTERRUPT
113  );
114  //Enable Receive interrupt
115  EUSCI_B_SPI_enableInterrupt(EUSCI_B0_BASE,
116  EUSCI_B_SPI_RECEIVE_INTERRUPT
117  );
118 
119  __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts
120 }
121 
122 //******************************************************************************
123 //
124 //This is the USCI_B0 interrupt vector service routine.
125 //
126 //******************************************************************************
127 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
128 #pragma vector=USCI_B0_VECTOR
129 __interrupt
130 #elif defined(__GNUC__)
131 __attribute__((interrupt(USCI_B0_VECTOR)))
132 #endif
133 void USCI_B0_ISR (void)
134 {
135  switch(__even_in_range(UCB0IV, USCI_SPI_UCTXIFG))
136  {
137  case USCI_SPI_UCRXIFG: // UCRXIFG
138  //USCI_B0 TX buffer ready?
139  while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE,
140  EUSCI_B_SPI_TRANSMIT_INTERRUPT
141  ));
142 
143  //Transmit data to master
144  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE,
146  );
147 
148  //Receive data from master
149  receiveData = EUSCI_B_SPI_receiveData(EUSCI_B0_BASE);
150 
151  //Increment data to be transmitted
152  transmitData++;
153  break;
154  default:
155  break;
156  }
157 }
158 
uint8_t receiveData
void main(void)
void USCI_B0_ISR(void)
uint8_t transmitData
MPU_initThreeSegmentsParam param